package com.avertymaroc.library;

import java.util.ArrayList;
import java.util.HashMap;

import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.avertymaroc.app.R;
import com.avertymaroc.beans.SondageRapide;
import com.avertymaroc.beans.User;
import com.avertymaroc.users.SondagesRapides;
 
public class MyAdapter extends BaseAdapter {
 
    private Activity activity;
    private ArrayList<HashMap<String, Object>> data;
    private static LayoutInflater inflater=null;
    public ImageLoader imageLoader; 
 
    public MyAdapter(Activity a, ArrayList<HashMap<String, Object>> d) {
        activity = a;
        data=d;
        inflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        imageLoader=new ImageLoader(activity.getApplicationContext());
    }
    public void addData(ArrayList<HashMap<String, Object>> d){
    	data.addAll(d);
    	
    }
 
    public int getCount() {
        return data.size();
    }
 
    public Object getItem(int position) {
        return position;
    }
 
    public long getItemId(int position) {
        return position;
    }
 
    public View getView(int position, View convertView, ViewGroup parent) {
        View vi=convertView;
        if(convertView==null)
            vi = inflater.inflate(R.layout.list_row_sondages_rap, null);
 
        TextView question = (TextView)vi.findViewById(R.id.question); 
        TextView prenom = (TextView)vi.findViewById(R.id.user_name); 
        TextView nb_votes = (TextView)vi.findViewById(R.id.nb_votes);
        TextView delai = (TextView)vi.findViewById(R.id.delai);
        ImageView user_image=(ImageView)vi.findViewById(R.id.user_image);
        
        HashMap<String, Object> sondage_priv = new HashMap<String, Object>();
        sondage_priv = data.get(position);
      
        
        question.setText( (CharSequence) sondage_priv.get(SondageRapide.KEY_TITRE) );
        prenom.setText( (CharSequence) sondage_priv.get(User.KEY_PRENOM) );
        nb_votes.setText( (CharSequence) sondage_priv.get(SondageRapide.KEY_VOTES) );
        delai.setText((CharSequence) sondage_priv.get(SondageRapide.KEY_DUREE));
        imageLoader.DisplayImage((String )sondage_priv.get(User.KEY_IMAGE), user_image);
        return vi;
    }
}